<template>
  <div class="profile-container">
    <div class="header">
      <div class="user-info">
        <div class="avatar">
          <img src="../../assets/logo.png" alt="用户头像">
        </div>
        <div class="name-role">
          <h2>{{ name }}</h2>
          <p>{{ role }}</p>
        </div>
      </div>
      <div class="settings-btn">
        <van-icon name="setting" size="20" />
      </div>
    </div>

    <div class="profile-section">
      <h3>个人信息</h3>
      <div class="info-item">
        <span class="label">学号</span>
        <span class="value">{{ studentId }}</span>
      </div>
      <div class="info-item">
        <span class="label">班级</span>
        <span class="value">{{ className }}</span>
      </div>
      <div class="info-item">
        <span class="label">邮箱</span>
        <span class="value">{{ email }}</span>
      </div>
      <div class="info-item">
        <span class="label">电话</span>
        <span class="value">{{ phone }}</span>
      </div>
    </div>

    <div class="profile-section">
      <h3>学习数据</h3>
      <div class="stats-grid">
        <div class="stat-item">
          <div class="stat-value">{{ averageScore }}</div>
          <div class="stat-label">平均成绩</div>
        </div>
        <div class="stat-item">
          <div class="stat-value">{{ examCount }}</div>
          <div class="stat-label">已参加考试</div>
        </div>
        <div class="stat-item">
          <div class="stat-value">{{ certificateCount }}</div>
          <div class="stat-label">获得证书</div>
        </div>
      </div>
    </div>

    <div class="action-buttons">
      <van-button type="primary" block @click="editProfile">修改资料</van-button>
      <van-button type="default" block>退出登录</van-button>
    </div>

    <!-- 底部导航 -->
    <van-tabbar v-model="activeTab" class="bottom-tabbar">
      <van-tabbar-item icon="home-o" text="首页" to="/home" />
      <van-tabbar-item icon="search" text="考试" to="/examList" />
      <van-tabbar-item icon="edit" text="日报" to="/dailyReport" />
      <van-tabbar-item icon="chat" text="论坛" to="/forum" />
      <van-tabbar-item icon="user" text="我的" to="/profile" />
    </van-tabbar>
  </div>
</template>

<script>

export default {
  name: 'ProfilePage',
  data() {
    return {
      activeTab: 4,
      name: '加载中...',
      role: '',
      studentId: '',
      className: '',
      email: '',
      phone: '',
      averageScore: 0,
      examCount: 0,
      certificateCount: 0
    };
  },
  methods: {
    editProfile() {
      this.$router.push('/editProfile');
    }
  },
  mounted() {
    try {
      const user = JSON.parse(localStorage.getItem('user') || '{}');
      if (user && user.name) {
        this.name = user.name;
        this.role = user.role || '';
        this.studentId = user.id || '';
        this.className = user.classId ? "软件工程" + user.classId + "班" : '';
        this.email = user.email || '';
        this.phone = user.phone || '';
        // 设置学习数据
        this.averageScore = user.averageScore || 0;
        this.examCount = user.examCount || 0;
        this.certificateCount = user.certificateCount || 0;
      } else {
        console.warn('未找到有效的用户信息');
      }
    } catch (error) {
      console.error('解析用户信息失败:', error);
    }
  },

};
</script>

<style scoped>
.profile-container {
  min-height: 100vh;
  background: linear-gradient(to bottom, #f5f7fa, #e4e9f2);
  padding-bottom: 60px;
}

.header {
  background-color: #fff;
  padding: 20px 16px;
  display: flex;
  justify-content: space-between;
  align-items: center;
  box-shadow: 0 2px 10px rgba(0, 0, 0, 0.05);
}

.user-info {
  display: flex;
  align-items: center;
}

.avatar {
  width: 60px;
  height: 60px;
  border-radius: 50%;
  overflow: hidden;
  margin-right: 15px;
  border: 2px solid #f0f0f0;
}

.avatar img {
  width: 100%;
  height: 100%;
  object-fit: cover;
}

.name-role h2 {
  font-size: 18px;
  font-weight: bold;
  margin: 0;
  color: #333;
}

.name-role p {
  font-size: 14px;
  color: #666;
  margin: 2px 0 0;
}

.settings-btn {
  color: #666;
}

.profile-section {
  background-color: #fff;
  margin: 12px 16px;
  border-radius: 12px;
  padding: 16px;
  box-shadow: 0 2px 10px rgba(0, 0, 0, 0.05);
}

.profile-section h3 {
  font-size: 16px;
  color: #333;
  margin: 0 0 16px;
  padding-bottom: 8px;
  border-bottom: 1px solid #f0f0f0;
}

.info-item {
  display: flex;
  justify-content: space-between;
  padding: 12px 0;
  border-bottom: 1px solid #f0f0f0;
}

.info-item:last-child {
  border-bottom: none;
}

.label {
  font-size: 14px;
  color: #666;
}

.value {
  font-size: 14px;
  color: #333;
  text-align: right;
}

.stats-grid {
  display: grid;
  grid-template-columns: repeat(3, 1fr);
  gap: 10px;
}

.stat-item {
  text-align: center;
  padding: 10px;
  background-color: #f9fafb;
  border-radius: 8px;
}

.stat-value {
  font-size: 22px;
  font-weight: bold;
  color: #333;
  margin-bottom: 4px;
}

.stat-label {
  font-size: 12px;
  color: #666;
}

.action-buttons {
  padding: 0 16px;
  margin-top: 20px;
}

.action-buttons .van-button {
  margin-bottom: 12px;
  border-radius: 25px;
  height: 45px;
  line-height: 45px;
  font-size: 16px;
}

.bottom-tabbar {
  background-color: white;
  position: fixed;
  bottom: 0;
  left: 0;
  right: 0;
  z-index: 100;
  box-shadow: 0 -2px 10px rgba(0, 0, 0, 0.05);
}
</style>